package nci60.data.maxquant.msms
import nci60.data.maxquant.RawFile
import nci60.data.maxquant.Experiment
import nci60.data.maxquant.Project
import nci60.data.maxquant.peptide.Peptide

/**
 * Ms/Ms spectrum information from MaxQuant analysis <p />
 * (source: msms.txt)
 * 
 * @autor <a href="mailto:Florian.J.Auer@googlemail.com">Florian J. Auer</a>
*/
class MsMs {

    /**
    * reference to the RAW file the mass spectral data was read from <p />
    * @see nci60.data.maxquant.RawFile
    */
    RawFile rawFile
    
    /**
    * Reference to the cell line (experiment) this msms information belongs to <p />
    * @see nci60.data.maxquant.Experiment
    */
    Experiment experiment
    
    /**
    * reference to the analysis it belongs to <p />
    * @see nci60.data.maxquant.Project
    */
    Project currentProject
    
    /**
    * The RAW-file derived scan number of the MS/MS spectrum
    */
    Integer scanNumber
    
    /**
    * The identified AA sequence of the peptide
    */
    String sequence
    
    /**
    * Number of missed enzymatic cleavages
    */
    Integer missedCleavages
    
    /**
    * Shows, if the peptide was acetylated at the N-terminus
    */
    Boolean acetylation
    
    /**
    * Shows the oxidation sites
    */
    String oxidations
    
    /**
    * The charge state of the precursor ion
    */
    Integer charge
    
    /**
    * Number of the scan event
    */
    Integer scanEventNumber
    
    /**
    * Isotope index
    */
    Integer isotopeIndex
    
    /**
    * The mass-over-charge of the precursor ion
    */
    Double precursorMZ
    
    /**
    * The charge corrected mass of the precursor ion
    */
    Double precursorMass
    
    /**
    * Mass error of the recalibrated mass-over-charge value of the precursor ion in comparison to the predicted monoisotopic mass (ppm) of the identified peptide sequence <p />
    * This property is nullable! <p />
    * <b>See Also:</b> {@link #mapping}
    */
    Double precursorMassError
    
    /**
    * The intensity of the precursor ion at the scannumber it was selected
    */
    Double precursorIntensity
    
    /**
    * Posterior Error Probability of the identification. This value essentially operates as a p-value, where smaller is better
    */
    Double pep
    
    /**
    * Andromeda score for the best associated MS/MS spectrum
    */
    Double score
    
    /**
    * Score difference to the second best identified peptide
    */
    Double deltaScore
    
    /**
    * id used in MaxQuant output file
    */
    Integer idInFile
    
    
    /**
    * GRAILS <a href="http://grails.org/doc/latest/guide/GORM.html#gormAssociation" target="_blank">Associations in Gorm</a>
    * <p />
    * <style>.custom-table td{border-top:1pt solid grey;vertical-align:top;} .custom-table{border-collapse:collapse;width:100%;}</style>
    * <table class="custom-table">
    * <tr><td>
    *              peptide
    * </td><td>        belongs to one specific peptide entry<p />
    *                  <b>See Also:</b> {@link nci60.data.maxquant.peptide.Peptide}
    * </td></tr>
    * </table>
    */
    static belongsTo = [peptide:Peptide]


    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/GORM.html#ormdsl" target="_blank">Custom ORM Mapping</a>
     * <p />
     * Grails domain classes can be mapped onto many legacy schemas with an Object Relational Mapping DSL (domain specific language). 
     */	
    static mapping = {
        version false
    }


    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/single.html#constraints" target="_blank">Declaring Constraints</a>
     * <p />
     * Constraints in Grails are a way to declaratively specify validation rules for generating objects in GORM.
     */	
    static constraints = {
        precursorMassError nullable:true
    }

    /**
     * Transforms the attribute values from the MaxQuant input file to values suitable for sql. e.g. "+", and "", are converted into "true", and "false" respectively. <p />
     * This mehtod is used while importing new data.
     * 
     * @param property  name of the property of this class (specified above)
     * @param input     attribute value in the MaxQuant output file
     * @return  replaced sql value of the input
     */		
    static String modifySQL(String property, String input) {
        String output
        if((property=='sequence')||(property=='msmsMassDeviations')||(property=='msmsMasses')||(property=='msmsIntensities')||(property=='oxidations')){
            output = '\"'+input+'\"'
        }else if(property=='precursorMassError'){
            output = (input=='NaN')?'null':input
        }else{
            output = input
        }
        return output
    }
}
